package com.dingwen.wcrtst.controller;

import com.dingwen.wcrtst.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Test
 *
 * @author dingwen
 * 2021.05.07 15:53
 */
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {


    /*
     *  不带参数的get请求
     * @return String
     */
    @GetMapping("/get01")
    public String get01() {
        return "get01() ok";
    }


    /*
     * 带请求参数的get请求
     * @param pageSize
     * @param pageNum
     * @return String
     */
    @GetMapping("/get02")
    public String get02(@RequestParam Integer pageSize, @RequestParam Integer pageNum) {
        return "get02() ok,请求参数为：" + pageSize + "/" + pageNum;
    }

    /*
     * 带路径参数的get请求
     * @param id
     * @return String
     */
    @GetMapping("/get03/{id}")
    public String get03(@PathVariable("id") String id) {
        return "get03() ok,路径参数为：" + id;
    }


    /*
     * 带有请求参数和路径参数的get请求
     * @param score
     * @param id
     * @return String
     */
    @GetMapping("/get04/{id}")
    public String get04(@RequestParam String score, @PathVariable("id") String id) {
        return "get04() ok,请求参数为：" + score + "，路径参数为：" + id;
    }

    /*
     * 模拟返回对象集合的GET请求，需要请求参数，携带token(过滤器实现：检查请求头)
     * @param pageSize
     * @param pageNum
     * @return User>
     */
    @GetMapping("/get05")
    public List<User> get05(@RequestParam("pageSize") Integer pageSize,
                            @RequestParam("pageNum") Integer pageNum, HttpServletRequest request) {
        List<User> userList = new ArrayList<>();
        userList.add(User.builder().id("1111").password("1111").build());
        userList.add(User.builder().id("2222").password("2222").build());
        String token = request.getHeader("token");
        log.info("token:{}", token);
        log.info("pageSize={},pageNum={}", pageSize, pageNum);
        return userList;
    }

    /*
     * 只有请求体的POST请求
     * @param user
     *
     */
    @PostMapping("/post01")
    public User post01(@RequestBody User user) {
        log.info("请求体：{}", user.toString());
        return user;
    }


        /*
         * 带有请求体参数、请求参数、路径参数的POST请求
         * @param user
         * @param status
         * @param type
         * @return String
         */
        @PostMapping("/post02/{type}")
        public String post02(@RequestBody User user,
                             @RequestParam("status") Integer status,
                             @PathVariable("type") String type) {
            return "requestBody:" + user.toString() + ",requestParam:" + status + ",pathVariable:" + type + "";
        }
}
